"""
bar_label的使用
"""

import matplotlib.pyplot as plt
import numpy as np

people = ("Tom", "Dick", "Harry", "Slim", "Jim")
y_pos = np.array([0, 1, 2, 3, 4])
performance = 3 + 10 * np.random.rand(len(people))
error = np.random.rand(len(people))

fig, ax = plt.subplots()
hbars = ax.barh(y_pos, performance, height=0.5, xerr=error, align="center")
ax.set_yticks(y_pos, labels=people)
ax.invert_yaxis()  # labels read top-to-bottom
ax.set_xlabel("Performance")
ax.set_title("How fast do you want to go today?")

# Label with given captions, custom padding and annotate options
# ax.bar_label(hbars, labels=['±{}'.format(round(e,3)) for e in error],
ax.bar_label(
    hbars,
    labels=[f"±{e:.2f}" for e in error],
    padding=5,
    label_type="edge",
    color="k",
    fontsize=10,
)
# ax.set_xlim(right=20)
# ax.set_xlim(None,16)
ax.set_xlim(None, 16)
plt.show()
